package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result page(String name,Integer subject,Integer target,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult<Course> pageResult = courseService.page(name,subject,target,page,pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id){
        courseService.delectById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @GetMapping("/list")
    public Result list(){
        List<Course> courseList = courseService.getList();
        return Result.success(courseList);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable("subject") Integer subject){
        List<Course> list = courseService.getBySub(subject);
        return Result.success(list);
    }

}
